package com.zcy.web;
import com.zcy.util.AjaxResult;
import com.zcy.domain.TenantMeal;
import com.zcy.query.TenantMealQuery;
import com.zcy.service.impl.TenantMealServiceImpl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.PageList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zheng'chuan'yin
 * @since 2020-11-27
 */
@RestController
@RequestMapping("/tenantMeal")
public class TenantMealController {
    @Autowired
    public TenantMealServiceImpl tenantMealService;

    /**
     * 保存和修改公用的
     * @param tenantMeal 传递的实体
     * @return Ajaxresult转换结果
     */
    @ApiOperation(value = "通用保存接口")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public AjaxResult save(@RequestBody TenantMeal tenantMeal) {
        try {
            if (tenantMeal.getMealId() != null){
                    tenantMealService.updateById(tenantMeal);
            }else{
                    tenantMealService.save(tenantMeal);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @ApiOperation(value = "通用删除接口")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
                tenantMealService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！" + e.getMessage());
        }
    }

    //获取对象信息
    @ApiOperation(value = "通用根据ID查询对象信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public TenantMeal get(@PathVariable("id") Long id) {
        return tenantMealService.getById(id);
    }


    /**
    * 查看所有的对象信息
    * @return
    */
    @ApiOperation(value = "通用查询所有")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List<TenantMeal> list() {

        return tenantMealService.list();
    }


    /**
    * 分页查询数据(不带查询条件))
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @ApiOperation(value = "通用分页查询（不带条件）")
    @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
    public PageList<TenantMeal> pageList(@RequestBody TenantMealQuery query) {
        Page<TenantMeal> page = new Page<TenantMeal>(query.getPage(), query.getRows());
        page = tenantMealService.page(page);
        return new PageList<TenantMeal>(page.getRecords(), page.getTotal());
    }
}
